<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @opened="opened" @close="close" destroy-on-close>
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
			<el-row>
				<el-col :span="24">
					<el-form-item label="产品名称" prop="name" :rules="rules.validateRequire">
						<el-input v-model="model.name" :disabled="publishDisabled"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="24">
					<el-form-item label="所属产品线" prop="productline" :rules="rules.validateRequire">
						<el-select v-model="model.productline" filterable clearable :disabled="publishDisabled">
							<el-option v-for="item in codeModel.productlineList" :key="item.code" :label="item.name" :value="item.code" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="类别1" prop="catalog1" :rules="rules.validateRequire">
						<el-select v-model="model.catalog1" filterable clearable :disabled="publishDisabled">
							<el-option v-for="item in codeModel.catalog1List" :key="item.code" :label="item.name" :value="item.code" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="类别2" prop="catalog2" :rules="rules.validateRequire">
						<el-select v-model="model.catalog2" filterable clearable :disabled="publishDisabled">
							<el-option v-for="item in codeModel.catalog2List" :key="item.code" :label="item.name" :value="item.code" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="相关部门" prop="deptcodearray">
						<tl-ref-dept v-model="model.deptcodearray" :initDisplayData.sync="model.dispdeptcodearray" dataType="code" nameType="fullname" :singleSelect="false"></tl-ref-dept>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="行销分类" prop="marketingclassification" :rules="rules.validateRequire">
						<el-select v-model="model.marketingclassification" filterable clearable>
							<el-option v-for="item in codeModel.marketingclassificationList" :key="item.code" :label="item.name" :value="item.code" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="排序码" prop="sortcode">
						<el-input-number v-model="model.sortcode" controls-position="right" :step="1" :min="0" style="width:100%"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :span="12"  v-if="!isAdd">
					<el-form-item label="启用" prop="enabled">
						<el-radio-group v-model="model.enabled" :disabled="disableEditSubmit || model.publishstate === '0'">
							<el-radio :label="parseInt(item.code)" v-for="item in codeModel.enabledList" :key="item.code">{{ item.name }}</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="24">
					<el-form-item label="备注" prop="description" >
						<el-input v-model="model.description" type="textarea" :rows="4" :disabled="publishDisabled"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
			<el-button type="primary" @click="publishProduct" :loading="saveLoading" v-if="disableEditSubmit === false && model.id && model.publishstate === '0'">发布</el-button>
			<el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">{{isAdd || model.publishstate === '0' ? '暂存' : '确认'}}</el-button>
    </div>
		<cataLog3Edit ref="cataLog3EditRef" :productInfo="model" v-show="model.publishstate === '1'"></cataLog3Edit>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 子组件 */
import cataLog3Edit from '../mmsProductFloorPriceInfo/indexComp'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { cataLog3Edit },
  data() {
    return {
      moduleTitle: '产品基础信息',
      urlPrefix: '/mms/productBaseInfo/',
      url: {
        // 额外的url
					publishUrl: '/mms/productBaseInfo/publish'
      }
    }
  },
	computed: {
      publishDisabled() {
          if (this.model.publishstate && this.model.publishstate === '1') {
              return true
					}
          return false
      }
	},
  methods: {
    loaded() {
      // 页面加载完毕执行
			this.$refs.cataLog3EditRef.doQuery()
    },
		beforeSubmit() {
			this.model.deptcodearrayname = this.model.dispdeptcodearray
			return true
		},
		publishProduct() {
        this.saveLoading = true
        getAction(this.url.publishUrl, { id: this.model.id }).then((res) => {
            if (res.success) {
                this.$tap.message('发布成功', 'success')
                this.visible = false
                this.$emit('refresh')
            } else {
                this.$tap.alert(res.message, 'warning')
            }
            this.saveLoading = false
        }).catch((err) => {
            this.saveLoading = false
            this.$tap.alert(err.message, 'warning')
        })
		},
		doAdd() {
				this.saveLoading = true
				postAction(this.url.add + '?billuid=' + this.uid, this.model)
						.then((res) => {
								if (res.success) {
										this.$tap.message('添加成功', 'success')
										this.model = res.result
										this.isAdd = false
										this.$emit('refresh')
								} else {
										this.$tap.alert(res.message, 'warning')
								}
								this.afterSubmit()
								this.saveLoading = false
						})
						.catch((err) => {
								this.saveLoading = false
						})
		},
		doEdit() {
				this.saveLoading = true
				putAction(this.url.edit, this.model)
						.then((res) => {
								if (res.success) {
										this.$tap.message(res.message, 'success')
										this.$emit('refresh')
								} else {
										this.$tap.alert(res.message, 'warning')
								}
								this.afterSubmit()
								this.saveLoading = false
						})
						.catch((err) => {
								this.saveLoading = false
						})
		},
		opened() {
			this.$refs.cataLog3EditRef.disableEditSubmit = this.disableEditSubmit
		}
  }
}
</script>

